package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.common.DateUtil;
import com.sofwin.mapper.SysUserMapper;
import com.sofwin.pojo.ListUtil;
import com.sofwin.pojo.SysUser;
import com.sofwin.pojo.SysUserExample;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @author: andyliu
 * @date: 2022/8/4 15:19
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper mapper;

    @Override
    public boolean saveUser(SysUser user) {
        if(user.getId()!=null){
            return mapper.updateByPrimaryKeySelective(user)==1?true:false;
        }
        user.setCreateDate(DateUtil.getCurrent("yyyy-MM-dd"));
        return mapper.insertSelective(user)==1?true:false;
    }

    @Override
    public boolean removeUserById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public SysUser queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysUser> queryByPage(SysUser user, Integer roleId, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<SysUser> users = mapper.selectUsers(user,roleId);
        return new PageInfo<>(users);
    }

    @Override
    public boolean removeUserByIds(Integer[] ids) {
        return mapper.deleteByIds(ids) == ids.length;
    }

    @Override
    public SysUser checkLogin(SysUser user) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(user.getUserName());
        criteria.andPwdEqualTo(user.getPwd());
        List<SysUser> users = mapper.selectByExample(example);
        if(users!=null && users.size()==1){
            return users.get(0);
        }
        return null;
    }

    @Override
    public List<ListUtil> queryListUtil(Integer id) {
        List<ListUtil> util = mapper.selectListUtil(id);
        return util;
    }
}
